// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2024-08-20

package gen_annotation

import (
	"fmt"
	"regexp"
	"strconv"
	"strings"
)

var (
	RegexAnnotationAddQuotes        = regexp.MustCompile(`,([^"]*),`)
	RegexAnnotationRemoveCommaSpace = regexp.MustCompile(`\s*,\s*`)
)

type (
	// Annotation
	// is a type name for @ definition.
	Annotation string

	AnnotationItem struct {
		Annotation Annotation
		value      string
		values     []string
	}
)

const (
	AnnotationAny     Annotation = "any"
	AnnotationDelete  Annotation = "delete"
	AnnotationGet     Annotation = "get"
	AnnotationHead    Annotation = "head"
	AnnotationOptions Annotation = "options"
	AnnotationPatch   Annotation = "patch"
	AnnotationPost    Annotation = "post"
	AnnotationPut     Annotation = "put"

	AnnotationCron               Annotation = "cron"
	AnnotationCronGloballyUnique Annotation = "crongloballyunique"
	AnnotationCronRunOnStartup   Annotation = "cronrunonstartup"
	AnnotationCronUnique         Annotation = "cronunique"
	AnnotationMiddleware         Annotation = "middleware"
	AnnotationName               Annotation = "name"
	AnnotationRoutePrefix        Annotation = "routeprefix"
)

func (o Annotation) String() string {
	return string(o)
}

func (o Annotation) ToUpper() string {
	return strings.ToUpper(o.String())
}

func NewAnnotationItem(anno Annotation, value string) *AnnotationItem {
	return (&AnnotationItem{
		Annotation: anno,
		value:      value,
		values:     make([]string, 0),
	}).init()
}

func (o *AnnotationItem) First() (str string) {
	if len(o.values) > 0 {
		return o.values[0]
	}
	return ""
}

func (o *AnnotationItem) FirstBool() bool {
	if s := o.First(); s != "" {
		if v, err := strconv.ParseBool(s); err == nil {
			return v
		}
	}
	return false
}

func (o *AnnotationItem) Values() []string {
	return o.values
}

func (o *AnnotationItem) init() *AnnotationItem {
	// Remove
	// comma space and add end comma.
	o.value = "," + RegexAnnotationRemoveCommaSpace.ReplaceAllString(o.value, ",") + ","

	// Add
	// each part with quotes.
	o.value = RegexAnnotationAddQuotes.ReplaceAllStringFunc(o.value, func(s string) string {
		if m := RegexAnnotationAddQuotes.FindStringSubmatch(s); len(m) > 0 {
			return fmt.Sprintf(`,"%s",`, m[1])
		}
		return s
	})

	// Remove start and end comma.
	o.value = strings.TrimPrefix(o.value, ",")
	o.value = strings.TrimSuffix(o.value, ",")

	// Split value as a slice.
	for _, s := range strings.Split(o.value, `","`) {
		if s = strings.TrimSpace(s); s != "" {
			s = strings.TrimPrefix(s, `"`)
			s = strings.TrimSuffix(s, `"`)
			s = strings.TrimSpace(s)
		}
		o.values = append(o.values, s)
	}
	return o
}
